# pip install pandas numpy plotly
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
# 示例数据：生成一个简单的 3D 数据集
np.random.seed(42)
data = {
    'X': np.random.rand(100),
    'Y': np.random.rand(100),
    'Z': np.random.rand(100)
}
df = pd.DataFrame(data)
# 绘制简单的 3D 散点图
fig = px.scatter_3d(df, x='X', y='Y', z='Z', title='简单的 3D 散点图')
fig.show()
# 读取数据
state_abbrevs = pd.read_csv('state-abbrevs.csv')
# 选择用于 3D 可视化的字段
df_3d = state_abbrevs[['Population', 'Income', 'Life.Exp']]
# 绘制 3D 散点图
fig = px.scatter_3d(df_3d, x='Population', y='Income', z='Life.Exp',
                    title='3D 散点图：人口、收入与预期寿命',
                    labels={'Population': '人口', 'Income': '收入', 'Life.Exp': '预期寿命'})
fig.show()
# 示例数据：假设 state-population.csv 中有多个年份的数据
# 生成示例数据
data = {
    'STATE': ['Alabama', 'Alabama', 'Alaska', 'Alaska', 'Arizona', 'Arizona'],
    'YEAR': [2018, 2019, 2018, 2019, 2018, 2019],
    'POPESTIMATE': [4881846, 4903185, 729755, 731545, 7171645, 7278717]
}
df_line = pd.DataFrame(data)
# 绘制折线图
fig = px.line(df_line, x='YEAR', y='POPESTIMATE', color='STATE',
              title='各州人口估计值随时间变化',
              labels={'YEAR': '年份', 'POPESTIMATE': '人口估计值', 'STATE': '州'})
fig.show()
# 读取数据
state_abbrevs = pd.read_csv('state-abbrevs.csv')
# 绘制散点图
fig = px.scatter(state_abbrevs, x='Population', y='Income', color='state.region',
                 title='各州人口与收入散点图',
                 labels={'Population': '人口', 'Income': '收入', 'state.region': '地区'})
fig.show()